package cn.iocoder.an.module.mold.controller.admin.linestopcategory;

import cn.iocoder.an.module.mold.controller.admin.linestopregistration.vo.LinestopRegistrationRespVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.linestopcategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.linestopcategory.LinestopCategoryDO;
import cn.iocoder.an.module.mold.service.linestopcategory.LinestopCategoryService;

@Tag(name = "管理后台 - 停线类型")
@RestController
@RequestMapping("/mold/linestop-category")
@Validated
public class LinestopCategoryController {

    @Resource
    private LinestopCategoryService linestopCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建停线类型")
    @PreAuthorize("@ss.hasPermission('mold:linestop-category:create')")
    public CommonResult<Long> createLinestopCategory(@Valid @RequestBody LinestopCategorySaveReqVO createReqVO) {
        return success(linestopCategoryService.createLinestopCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新停线类型")
    @PreAuthorize("@ss.hasPermission('mold:linestop-category:update')")
    public CommonResult<Boolean> updateLinestopCategory(@Valid @RequestBody LinestopCategorySaveReqVO updateReqVO) {
        linestopCategoryService.updateLinestopCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除停线类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:linestop-category:delete')")
    public CommonResult<Boolean> deleteLinestopCategory(@RequestParam("id") Long id) {
        linestopCategoryService.deleteLinestopCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得停线类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:linestop-category:query')")
    public CommonResult<LinestopCategoryRespVO> getLinestopCategory(@RequestParam("id") Long id) {
        LinestopCategoryDO linestopCategory = linestopCategoryService.getLinestopCategory(id);
        return success(BeanUtils.toBean(linestopCategory, LinestopCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得停线类型列表")
    @PreAuthorize("@ss.hasPermission('mold:linestop-category:query')")
    public CommonResult<List<LinestopCategoryRespVO>> getLinestopCategoryList(@Valid LinestopCategoryListReqVO listReqVO) {
        List<LinestopCategoryDO> list = linestopCategoryService.getLinestopCategoryList(listReqVO);
        return success(BeanUtils.toBean(list, LinestopCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出停线类型 Excel")
    @PreAuthorize("@ss.hasPermission('mold:linestop-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLinestopCategoryExcel(@Valid LinestopCategoryListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<LinestopCategoryDO> list = linestopCategoryService.getLinestopCategoryList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "停线类型.xls", "数据", LinestopCategoryRespVO.class,
                        BeanUtils.toBean(list, LinestopCategoryRespVO.class));
    }

    @GetMapping("/getAllList")
    @Operation(summary = "获得所有设备分类")
    public CommonResult<List<LinestopCategoryDO>> getEquipmentTypeList() {
        return success(linestopCategoryService.getLineStopCategoryList());
    }
}